package com.zsy.springframework.beans.factory.chouxiang;

import cn.hutool.core.bean.BeanUtil;
import com.zsy.springframework.beans.factory.config.BeanDefinition;
import com.zsy.springframework.beans.factory.config.PropertyValue;
import com.zsy.springframework.beans.factory.config.PropertyValues;
import com.zsy.springframework.beans.factory.exception.BeansException;
import com.zsy.springframework.beans.factory.strategy.InstantiationStrategy;
import com.zsy.springframework.beans.factory.strategy.proxy.CglibSubClassingInstantiationStrategy;
import com.zsy.springframework.beans.factory.reference.BeanReference;

import java.lang.reflect.Constructor;

//在这里面实现如何create bean的操作，但是还是不完全，还需要后面的子类实现具体的细节
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {
    //这里涉及到bean创建的实例信息，是不是单例的策略

    private InstantiationStrategy instantiationStrategy = new CglibSubClassingInstantiationStrategy();

    public InstantiationStrategy getInstantiationStrategy() {
        return instantiationStrategy;
    }

    public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
        this.instantiationStrategy = instantiationStrategy;
    }

    @Override
    protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
        Object bean;
        try {
            bean = createBeanInstance(beanDefinition, beanName, args);
            //已经通过cglib创建完了bean对象 ，此时需要往这个bean里面添加一些常量等信息把
            //在这里进行bean的属性填充
            applyPropertyValues(beanName, bean, beanDefinition);
        } catch (Exception e) {
            throw new BeansException("Instantiation of bean failed", e);
        }

        addSingleton(beanName, bean);
        return bean;
    }

    protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
        try {
            PropertyValues propertyValues = beanDefinition.getPropertyValues();
            for (PropertyValue propertyValue : propertyValues.getPropertyValues()) {
                String name = propertyValue.getName();
                Object value = propertyValue.getValue();

                //依赖的问题
                //a依赖b，需要获取b的信息 这个value就是那个bean ！！！！！
                if (value instanceof BeanReference) {
                    BeanReference beanReference = (BeanReference) value;
                    value = getBean(beanReference.getBeanName());
                }
                BeanUtil.setFieldValue(bean, name, value);
            }
        } catch (Exception e) {
            throw new BeansException("Error setting property values：" + beanName);
        }
    }

    private Object createBeanInstance(BeanDefinition beanDefinition, String beanName, Object[] args) {
        Class beanClass = beanDefinition.getBeanClass();
        Constructor constructor = null;
        Constructor[] declaredConstructors = beanClass.getDeclaredConstructors();
        for (Constructor declaredConstructor : declaredConstructors) {
            //判断方法的参数个数，根arg的长度是否相同，他们都是数组
            if (args != null && declaredConstructor.getParameterTypes().length == args.length) {
                constructor = declaredConstructor;
                break;
            }
        }
        //拿到了构造器，开始创建
        //这里通过一个策略的设计 接口，然后后面调用的是cglib的方法实现！！！！！！！
        return getInstantiationStrategy().instantiate(beanDefinition, beanName, constructor, args);
    }
}
